﻿using LDT.CoreLibrary;
using LdtDataEngin.ModuleDetail.BaseModule.PersonnelBase;
using LdtDataEngin.ModuleDetail.PersonnelAnalysis;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.DataSave;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Personnel;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.Dudection;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.CommonModule.PersonMerger
{
   public class PersonnelDeductionData
    {
        /// <summary>
        /// 交互数据层
        /// </summary>
        public DataContext dynamicDataStores { get; set; }


        // <summary>
        /// 将人员代称替换为人员信息
        /// </summary>
        public void PersonnelOptimization(string xmlcofig, List<DataModel> dataModelList)
        {
            try
            {
                ElementExtractBase elementExtractBase = new ElementExtractBase() { dataContext = dynamicDataStores };
                elementExtractBase.LoadPersonnelInformation();
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(xmlcofig);
                XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/root/step");
                string config = "";
                if (xmlNodeList != null && xmlNodeList.Count > 0)
                {
                    string path = xmlNodeList[0].Attributes["executefile"].Value.Split(new string[] { @"wordparse2.0\" }, StringSplitOptions.RemoveEmptyEntries)[1];
                    Dictionary<string, string> dicElement = new Dictionary<string, string>();
                    dicElement.Add("fileFullName", path);
                    string elementMessage = WebUtil.DoGet(ConfigRetrieval.GetConfigRetrieval.GetAppSettingString("systemapi") + "/api/WordParse/GetConfigFileContent", dicElement, "utf-8");
                    dynamic elementcofig = JsonConvert.DeserializeObject<dynamic>(elementMessage);

                    if (elementcofig.result.code == "1")
                    {
                        config = elementcofig.body.ToString();
                    }
                }
                // PersonnelConvert_new(config, dataModelList);
                PersonnelConvert(config, dataModelList);
            }
            catch (Exception ex)
            {

                dynamicDataStores.InteractiveShow.LogShow("人员信息错误：" + ex.ToString());
            }
        }

        /// <summary>
        /// 进行替换操作
        /// </summary>
        /// <param name="config">配置文件</param>
        /// <param name="dataModelList">需要替换的案件要素</param>
        public void PersonnelConvert(string config, List<DataModel> dataModelList)
        {
            PersonnelConversionModel personnelConversionModel = XmlUtility.DeserializeToObject<PersonnelConversionModel>(config);
            if (personnelConversionModel != null)
            {
                PersonnelConversion personnelConversion = new PersonnelConversion() { spokenamelist = dynamicDataStores.DataLayerAns.ElementSpokeList };
                CaseModel caseModel = personnelConversionModel.caseModelList.FirstOrDefault(a => a.casetype.ToLower() == dynamicDataStores.DataLayerAns.CaseType.ToLower());
                if (caseModel != null)
                {
                    foreach (DataitemModel dataitemModel in caseModel.dataitemModelList)
                    {
                        string[] values = dataitemModel.path.Split(new string[] { "/@" }, StringSplitOptions.None);
                        string xmlpath = "";
                        string attname = "";
                        if (values.Length == 2)
                        {
                            xmlpath = values[0];
                            attname = values[1];
                        }
                        //转换为名称
                        DataModel dataModels = XmlUtility.ConditionFirstOrDefaultDataModel(dataModelList, "", xmlpath);
                        if (dataModels != null)
                        {
                            List<DataModel> dataList = XmlUtility.ConditionDataModelList(dataModelList, "", xmlpath);
                            foreach (DataModel dataModel in dataList)
                            {
                               AttributeInfo attribute = dataModel.AttributesList.FirstOrDefault(a => a.Name == attname);
                                if (attribute != null)
                                {
                                    Source source = attribute.sourceList.FirstOrDefault(a => a.IsCheck == "1");
                                    if (source != null)
                                    {
                                        if (dataitemModel.type == "1")
                                        {
                                            List<ElementSpokeName> nameModelList = personnelConversion.GetMatchSpokeManByRoleName(dynamicDataStores.DataLayerAns.ElementSpokeList, source.ExtValue.Replace("\r", ""), dynamicDataStores.DataLayerAns.CaseType).ToList();
                                            if (nameModelList.Count > 0)
                                            {

                                                if (!string.IsNullOrEmpty(nameModelList[0].ES_AssertorID) && dataitemModel.AssertorType == "1")
                                                {
                                                    ElementSpokeName elementSpokeName = dynamicDataStores.DataLayerAns.ElementSpokeList.FirstOrDefault(a => a.ES_ID == nameModelList[0].ES_AssertorID);

                                                    if (dataitemModel.tablefieldname.ToLower().ToString() == "ce_name")
                                                    {
                                                        source.SaveValue = elementSpokeName.ES_FullName;
                                                    }
                                                    else if (dataitemModel.tablefieldname.ToLower().ToString() == "etr_suitdegreeid")
                                                    {
                                                        source.SaveValue = elementSpokeName.ES_SuitDrgreeID;
                                                    }
                                                }
                                                else
                                                {
                                                    if (dataitemModel.tablefieldname.ToLower().ToString() == "ce_name")
                                                    {
                                                        source.SaveValue = nameModelList[0].ES_FullName;
                                                    }
                                                    else if (dataitemModel.tablefieldname.ToLower().ToString() == "etr_suitdegreeid")
                                                    {
                                                        source.SaveValue = nameModelList[0].ES_SuitDrgreeID;
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            source.SaveValue = personnelConversion.ReplaceShortNameToOriginalText(dynamicDataStores.DataLayerAns.ElementSpokeList, source.SaveValue, dynamicDataStores.DataLayerAns.CaseType);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                #region 保存数据
                XmlUtilCommon xmlUtilCommon = new XmlUtilCommon();
                DataSoursModel dataSoursModel = new DataSoursModel();
                dataSoursModel.dataModelList = dynamicDataStores.DataLayerOpt.SourdatModleList;
                XmlDocument xmlDocumentOp = new XmlDocument();
                xmlDocumentOp = xmlUtilCommon.DataModelByXml(dynamicDataStores.DataLayerOpt.SourdatModleList, "");
                dynamicDataStores.DataLayerOpt.OptimizationXml = xmlDocumentOp;
                #endregion

            }
        }

        //public void PersonnelConvert_new(string config, List<DataModel> dataModelList)
        //{
        //    string xmlconfig = "";
        //    Dictionary<string, string> dicElement = new Dictionary<string, string>();
        //    dicElement.Add("fileFullName", @"config\optimizationcfg\casenamededuce\Pronoun.xml");
        //    string elementMessage = WebUtil.DoGet(ConfigRetrieval.GetConfigRetrieval.GetConnectionString("systemapi") + "/api/WordParse/GetConfigFileContent", dicElement, "utf-8");
        //    dynamic elementcofig = JsonConvert.DeserializeObject<dynamic>(elementMessage);

        //    if (elementcofig.result.code == "1")
        //    {
        //        xmlconfig = elementcofig.body.ToString();
        //    }
        //    PersonnelConversionModel personnelConversionModel = XmlUtility.DeserializeToObject<PersonnelConversionModel>(config);
        //    if (personnelConversionModel != null)
        //    {
        //        PersonnelConvertClass personnelConversion = new PersonnelConvertClass() { spokenamelist = dynamicDataStores.dataLayerStores.ElementSpokeList, config = xmlconfig, dynamicDataStores = dynamicDataStores };
        //        CaseModel caseModel = dynamicDataStores.dataLayerStores.personnelConversionModel.caseModelList.FirstOrDefault(a => a.casetype.ToLower() == dynamicDataStores.dataLayerStores.caseType.ToLower());
        //        if (caseModel != null)
        //        {
        //            foreach (DataitemModel dataitemModel in caseModel.dataitemModelList)
        //            {
        //                string[] values = dataitemModel.path.Split(new string[] { "/@" }, StringSplitOptions.None);
        //                string xmlpath = "";
        //                string attname = "";
        //                if (values.Length == 2)
        //                {
        //                    xmlpath = values[0];
        //                    attname = values[1];
        //                }
        //                //转换为名称
        //                DataModel dataModels = XmlUtility.ConditionFirstOrDefaultDataModel(dataModelList, "", xmlpath);
        //                if (dataModels != null)
        //                {
        //                    List<DataModel> dataList = XmlUtility.ConditionDataModelList(dataModelList, "", xmlpath);
        //                    foreach (DataModel dataModel in dataList)
        //                    {
        //                        Model.XMLConfigModel.DataStores.Attribute attribute = dataModel.AttributesList.FirstOrDefault(a => a.Name == attname);
        //                        if (attribute != null)
        //                        {
        //                            Source source = attribute.sourceList.FirstOrDefault(a => a.IsCheck == "1");
        //                            if (source != null)
        //                            {
        //                                if (dataitemModel.type == "1")
        //                                {
        //                                    List<ElementSpokeName> nameModelList = personnelConversion.GetMatchSpokeManByRoleName(dynamicDataStores.dataLayerStores.ElementSpokeList, source.ExtValue.Replace("\r", ""), dynamicDataStores.dataLayerStores.caseType).ToList();
        //                                    if (nameModelList.Count > 0)
        //                                    {

        //                                        if (!string.IsNullOrEmpty(nameModelList[0].ES_AssertorID) && dataitemModel.AssertorType == "1")
        //                                        {
        //                                            ElementSpokeName elementSpokeName = dynamicDataStores.dataLayerStores.ElementSpokeList.FirstOrDefault(a => a.ES_ID == nameModelList[0].ES_AssertorID);

        //                                            if (dataitemModel.tablefieldname.ToLower().ToString() == "ce_name")
        //                                            {
        //                                                source.SaveValue = elementSpokeName.ES_FullName;
        //                                            }
        //                                            else if (dataitemModel.tablefieldname.ToLower().ToString() == "etr_suitdegreeid")
        //                                            {
        //                                                source.SaveValue = elementSpokeName.ES_SuitDrgreeID;
        //                                            }
        //                                        }
        //                                        else
        //                                        {
        //                                            if (dataitemModel.tablefieldname.ToLower().ToString() == "ce_name")
        //                                            {
        //                                                source.SaveValue = nameModelList[0].ES_FullName;
        //                                            }
        //                                            else if (dataitemModel.tablefieldname.ToLower().ToString() == "etr_suitdegreeid")
        //                                            {
        //                                                source.SaveValue = nameModelList[0].ES_SuitDrgreeID;
        //                                            }
        //                                        }
        //                                    }
        //                                }
        //                                else
        //                                {
        //                                    source.SaveValue = personnelConversion.ReplaceShortNameToOriginalText(dynamicDataStores.dataLayerStores.ElementSpokeList, source.SaveValue, dynamicDataStores.dataLayerStores.caseType);
        //                                }
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //        }

        //        #region 保存数据
        //        XmlUtilCommon xmlUtilCommon = new XmlUtilCommon();
        //        DataResultSave dataResultSave = new DataResultSave() { dynamicDataStores = dynamicDataStores };
        //        DataSoursModel dataSoursModel = new DataSoursModel();
        //        dataSoursModel.dataModelList = dynamicDataStores.dataLayerStores.SourdatModleList;
        //        XmlDocument xmlDataSours = new XmlDocument();
        //        xmlDataSours.LoadXml(XmlUtility.SerializeToXml<DataSoursModel>(dataSoursModel));
        //        dynamicDataStores.dataLayerStores.SourdataXml = xmlDataSours;
        //        XmlDocument xmlDocumentOp = new XmlDocument();
        //        xmlDocumentOp = xmlUtilCommon.DataModelByXml(dynamicDataStores.dataLayerStores.SourdatModleList, "");
        //        dynamicDataStores.dataLayerStores.OptimizationXml = xmlDocumentOp;
        //        #endregion

        //    }
        //}
        /// <summary>
        /// 进行代称替换操作
        /// </summary>
        /// <param name="config">配置文件</param>
        /// <param name="dataModelList">需要替换的案件要素</param>
        public void PersonnelAnalysis(string config, List<DataModel> dataModelList)
        {
            PersonnelConversionModel personnelConversionModel = XmlUtility.DeserializeToObject<PersonnelConversionModel>(config);
            if (personnelConversionModel != null)
            {
                PersonnelConversion personnelConversion = new PersonnelConversion() { spokenamelist = dynamicDataStores.DataLayerAns.ElementSpokeList };
                CaseModel caseModel = personnelConversionModel.caseModelList.FirstOrDefault(a => a.sourcecode?.ToLower() == dynamicDataStores.DataLayerAns.SourceCode?.ToLower() || a.sourcecode?.ToLower() == dynamicDataStores.DataLayerAns.CaseType?.ToLower());
                if (caseModel != null)
                {
                    foreach (DataitemModel dataitemModel in caseModel.dataitemModelList)
                    {
                        string[] values = dataitemModel.path.Split(new string[] { "/@" }, StringSplitOptions.None);
                        string xmlpath = "";
                        string attname = "";
                        if (values.Length == 2)
                        {
                            xmlpath = values[0];
                            attname = values[1];
                        }
                        //转换为名称
                        DataModel dataModels = XmlUtility.ConditionFirstOrDefaultDataModel(dataModelList, "", xmlpath);
                        if (dataModels != null)
                        {
                            List<DataModel> dataList = XmlUtility.ConditionDataModelList(dataModelList, "", xmlpath);
                            foreach (DataModel dataModel in dataList)
                            {
                                AttributeInfo attribute = dataModel.AttributesList.FirstOrDefault(a => a.Name == attname);
                                if (attribute != null)
                                {
                                    Source source = attribute.sourceList.FirstOrDefault(a => a.IsCheck == "1");
                                    if (source != null)
                                    {
                                        if (dataitemModel.type == "1")
                                        {
                                            List<ElementSpokeName> nameModelList = personnelConversion.GetMatchSpokeManByRoleName(dynamicDataStores.DataLayerAns.ElementSpokeList, source.ExtValue.Replace("\r", ""), dynamicDataStores.DataLayerAns.CaseType).ToList();
                                            if (nameModelList.Count > 0)
                                            {

                                                if (!string.IsNullOrEmpty(nameModelList[0].ES_AssertorID) && dataitemModel.AssertorType != null && dataitemModel.AssertorType == "1")
                                                {
                                                    ElementSpokeName elementSpokeName = dynamicDataStores.DataLayerAns.ElementSpokeList.FirstOrDefault(a => a.ES_ID == nameModelList[0].ES_AssertorID);

                                                    if (dataitemModel.tablefieldname.ToLower().ToString() == "ce_name")
                                                    {
                                                        source.SaveValue = elementSpokeName?.ES_FullName;
                                                    }
                                                    else if (dataitemModel.tablefieldname.ToLower().ToString() == "etr_suitdegreeid")
                                                    {
                                                        source.SaveValue = elementSpokeName?.ES_SuitDrgreeID;
                                                    }
                                                }
                                                else
                                                {
                                                    if (dataitemModel.tablefieldname.ToLower().ToString() == "ce_name")
                                                    {
                                                        source.SaveValue = nameModelList[0].ES_FullName;
                                                    }
                                                    else if (dataitemModel.tablefieldname.ToLower().ToString() == "etr_suitdegreeid")
                                                    {
                                                        source.SaveValue = nameModelList[0].ES_SuitDrgreeID;
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            source.SaveValue = personnelConversion.ReplaceShortNameToOriginalText(dynamicDataStores.DataLayerAns.ElementSpokeList, source.SaveValue, dynamicDataStores.DataLayerAns.CaseType);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                #region 保存数据
                XmlUtilCommon xmlUtilCommon = new XmlUtilCommon();

                XmlDocument xmlDocumentOp = new XmlDocument();
                xmlDocumentOp = xmlUtilCommon.DataModelByXml(dataModelList, "");
                dynamicDataStores.DataLayerAns.AnalysisResultsXml = xmlDocumentOp;
                dynamicDataStores.DataLayerAns.AnalysisResultsDataModel.dataModelList = dataModelList;
                #endregion

            }
        }
    }
}
